<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="学期" prop="cterm">
            <el-select v-model="queryParams.cterm" placeholder="请选择学期" clearable style="width:160px">
              <el-option
                v-for="dict in initTermList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="课程名称" prop="cname">
            <el-input
              v-model="queryParams.cname"
              placeholder="请输入课程名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="选课状态" prop="scStatus">
            <el-select v-model="queryParams.scStatus" placeholder="请选择" clearable style="width:160px">
              <el-option
                v-for="dict in dict.type.jxgl_sc_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="teachingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学期" align="center" prop="cterm" >
        <template slot-scope="scope">
          <span>{{Math.floor(scope.row.cterm / 10)}}年.</span>
          <span v-if="scope.row.cterm%10==0">春</span>
          <span v-else-if="scope.row.cterm%10==1">秋</span>
        </template>
      </el-table-column>
      <el-table-column label="课程信息" align="left" width="240" >
        <template slot-scope="scope">
          {{scope.row.cno}} | {{scope.row.cname}}
        </template>
      </el-table-column>
      <el-table-column label="学时" align="center" prop="period" width="160" />
      <el-table-column label="教工信息" width="120">
        <template slot-scope="scope">
          {{scope.row.tno}} | {{scope.row.tname}} 
        </template>
      </el-table-column>
      <el-table-column label="职称" align="center" prop="ps">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jxgl_tea_ps" :value="scope.row.ps"/>
        </template>
      </el-table-column>
      <el-table-column label="选课状态" align="center" prop="isOpen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jxgl_sc_status" :value="scope.row.scStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.scStatus==0"
            @click="handleAdd(scope.row)" v-hasPermi="['jxgl:sc:add']" >我要选课</el-button>
          <el-button size="mini" type="plain" icon="el-icon-edit" v-if="scope.row.scStatus==1"
            @click="handleDel(scope.row)" v-hasPermi="['jxgl:sc:remove']" >我要退选</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>


<script>
import { listMyScTeaching, addSc,delSc} from "@/api/jxgl/sc";
export default {
  name: "Teaching",
  dicts: ['jxgl_sc_status','jxgl_tea_ps'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学任务表格数据
      teachingList: [],
      initTermList: [],
      // 课程列表
      courseList: [],
      // 教师列表
      teacherList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cterm: null,
        cname: null,
        scStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        cname: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        tno: [
          { required: true, message: "教工号不能为空", trigger: "blur" }
        ],
        isOpen: [
          { required: true, message: "开放选课？不能为空", trigger: "change" }
        ],
        isOpenScore: [
          { required: true, message: "开放录分？不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.makeInitTerms();
  },
  methods: {
    makeInitTerms(){
      var terms=[];
      var date = new Date();
      var yyyy = date.getFullYear();
      for(var i=yyyy+3;i>=yyyy-15;i--){
        terms.push({label:i+'年.秋',value:i*10+1});
        terms.push({label:i+'年.春',value:i*10});
      }
      this.initTermList=terms;
    },
    /** 查询教学任务列表 */
    getList() {
      this.loading = true;
      listMyScTeaching(this.queryParams).then(response => {
        this.teachingList = response.rows;
        console.log(this.teachingList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cterm: null,
        cno: null,
        cname: null,
        period: null,
        tno: null,
        tname: null,
        ps: null,
        isOpen: null,
        isOpenScore: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 选课操作 */
    handleAdd(row) {
      this.$modal.confirm('是否确认报选课程："' + row.cname + '"？').then(function() {
        return addSc({
          tid:row.id,
          cno:row.cno
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("选课成功");
      }).catch(() => {});
    },
     /** 退选操作 */
     handleDel(row) {
      this.$modal.confirm('是否确认退选课程："' + row.cname + '"？').then(function() {
        return delSc(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退选成功");
      }).catch(() => {});
    },
  }
};
</script>
